package vip.meeet.kafka;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

final public class Configuration {
	
	private static final Logger logger = LoggerFactory.getLogger(Configuration.class);
	
	private static Map<String,Properties> propCache = new HashMap<String,Properties>();
	
	public static Properties getPropertiesByName(String fileName)
	{
		Properties prop = propCache.get(fileName);
		if(prop == null)
		{
			synchronized(Configuration.class){
				if(prop == null)
				{
					prop = getPropertie(fileName);
					propCache.put(fileName, prop);
				}
			}
		}
		return prop;
	}
	
	private static Properties getPropertie(String name)
	{
		ClassPathResource config = new ClassPathResource(name);
		if(!config.exists())
			throw new IllegalArgumentException("请检查配置文件"+name+"路径是否正确");
		Properties prop = new Properties();
		try {
			prop.load(config.getInputStream());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		return prop;
	}

}
